<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程的id" prop="courseId">
      <el-input v-model="dataForm.courseId" placeholder="课程的id"></el-input>
    </el-form-item>
    <el-form-item label="课程的类型" prop="courseType">
      <el-input v-model="dataForm.courseType" placeholder="课程的类型"></el-input>
    </el-form-item>
    <el-form-item label="评论者的id" prop="commentId">
      <el-input v-model="dataForm.commentId" placeholder="评论者的id"></el-input>
    </el-form-item>
    <el-form-item label="被回复者的id，如果没有则为0" prop="replyId">
      <el-input v-model="dataForm.replyId" placeholder="被回复者的id，如果没有则为0"></el-input>
    </el-form-item>
    <el-form-item label="评论者的名字" prop="commentName">
      <el-input v-model="dataForm.commentName" placeholder="评论者的名字"></el-input>
    </el-form-item>
    <el-form-item label="回复者的名字" prop="replyName">
      <el-input v-model="dataForm.replyName" placeholder="回复者的名字"></el-input>
    </el-form-item>
    <el-form-item label="评论内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="评论内容"></el-input>
    </el-form-item>
    <el-form-item label="评论时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="评论时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除位" prop="showStatus">
      <el-input v-model="dataForm.showStatus" placeholder="逻辑删除位"></el-input>
    </el-form-item>
    <el-form-item label="是否显示该条评论" prop="isPublic">
      <el-input v-model="dataForm.isPublic" placeholder="是否显示该条评论"></el-input>
    </el-form-item>
    <el-form-item label="根评论的id" prop="rootComment">
      <el-input v-model="dataForm.rootComment" placeholder="根评论的id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          courseId: '',
          courseType: '',
          commentId: '',
          replyId: '',
          commentName: '',
          replyName: '',
          content: '',
          time: '',
          showStatus: '',
          isPublic: '',
          rootComment: ''
        },
        dataRule: {
          courseId: [
            { required: true, message: '课程的id不能为空', trigger: 'blur' }
          ],
          courseType: [
            { required: true, message: '课程的类型不能为空', trigger: 'blur' }
          ],
          commentId: [
            { required: true, message: '评论者的id不能为空', trigger: 'blur' }
          ],
          replyId: [
            { required: true, message: '被回复者的id，如果没有则为0不能为空', trigger: 'blur' }
          ],
          commentName: [
            { required: true, message: '评论者的名字不能为空', trigger: 'blur' }
          ],
          replyName: [
            { required: true, message: '回复者的名字不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '逻辑删除位不能为空', trigger: 'blur' }
          ],
          isPublic: [
            { required: true, message: '是否显示该条评论不能为空', trigger: 'blur' }
          ],
          rootComment: [
            { required: true, message: '根评论的id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/course/comments/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courseId = data.comments.courseId
                this.dataForm.courseType = data.comments.courseType
                this.dataForm.commentId = data.comments.commentId
                this.dataForm.replyId = data.comments.replyId
                this.dataForm.commentName = data.comments.commentName
                this.dataForm.replyName = data.comments.replyName
                this.dataForm.content = data.comments.content
                this.dataForm.time = data.comments.time
                this.dataForm.showStatus = data.comments.showStatus
                this.dataForm.isPublic = data.comments.isPublic
                this.dataForm.rootComment = data.comments.rootComment
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/comments/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'courseId': this.dataForm.courseId,
                'courseType': this.dataForm.courseType,
                'commentId': this.dataForm.commentId,
                'replyId': this.dataForm.replyId,
                'commentName': this.dataForm.commentName,
                'replyName': this.dataForm.replyName,
                'content': this.dataForm.content,
                'time': this.dataForm.time,
                'showStatus': this.dataForm.showStatus,
                'isPublic': this.dataForm.isPublic,
                'rootComment': this.dataForm.rootComment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
